Merge "ApiSandbox: Indicate when login is suppressed"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.apisandbox.js
index aff5f3b..ff6a80c 100644 (file)
                                this.setIcon( ok ? null : 'alert' );
                                this.setIconTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() );
                                return $.Deferred().resolve( ok ).promise();
+                       },
+                       createItemWidget: function ( data, label ) {
+                               var item = OO.ui.CapsuleMultiselectWidget.prototype.createItemWidget.call( this, data, label );
+                               if ( this.paramInfo.deprecatedvalues &&
+                                       this.paramInfo.deprecatedvalues.indexOf( data ) >= 0
+                               ) {
+                                       item.$element.addClass( 'apihelp-deprecated-value' );
+                               }
+                               return item;
                        }
                },
 
                                        } else if ( Util.apiBool( pi.multi ) ) {
                                                widget = new OO.ui.CapsuleMultiselectWidget( {
                                                        allowArbitrary: true,
-                                                       allowDuplicates: Util.apiBool( pi.allowsduplicates )
+                                                       allowDuplicates: Util.apiBool( pi.allowsduplicates ),
+                                                       $overlay: $( '#mw-apisandbox-ui' )
                                                } );
                                                widget.paramInfo = pi;
                                                $.extend( widget, WidgetMethods.capsuleWidget );
                                                }
 
                                                widget = new OO.ui.CapsuleMultiselectWidget( {
-                                                       menu: { items: items }
+                                                       menu: { items: items },
+                                                       $overlay: $( '#mw-apisandbox-ui' )
                                                } );
                                                widget.paramInfo = pi;
                                                $.extend( widget, WidgetMethods.capsuleWidget );
                                        } else {
                                                widget = new OO.ui.DropdownWidget( {
-                                                       menu: { items: items }
+                                                       menu: { items: items },
+                                                       $overlay: $( '#mw-apisandbox-ui' )
                                                } );
                                                widget.paramInfo = pi;
                                                $.extend( widget, WidgetMethods.dropdownWidget );
                                        }
 
                                        items = $.map( pi.type, function ( v ) {
-                                               return new OO.ui.MenuOptionWidget( { data: String( v ), label: String( v ) } );
+                                               var config = {
+                                                       data: String( v ),
+                                                       label: String( v ),
+                                                       classes: []
+                                               };
+                                               if ( pi.deprecatedvalues && pi.deprecatedvalues.indexOf( v ) >= 0 ) {
+                                                       config.classes.push( 'apihelp-deprecated-value' );
+                                               }
+                                               return new OO.ui.MenuOptionWidget( config );
                                        } );
                                        if ( Util.apiBool( pi.multi ) ) {
                                                if ( pi.allspecifier !== undefined ) {
                                                }
 
                                                widget = new OO.ui.CapsuleMultiselectWidget( {
-                                                       menu: { items: items }
+                                                       menu: { items: items },
+                                                       $overlay: $( '#mw-apisandbox-ui' )
                                                } );
                                                widget.paramInfo = pi;
                                                $.extend( widget, WidgetMethods.capsuleWidget );
                                                }
                                        } else {
                                                widget = new OO.ui.DropdownWidget( {
-                                                       menu: { items: items }
+                                                       menu: { items: items },
+                                                       $overlay: $( '#mw-apisandbox-ui' )
                                                } );
                                                widget.paramInfo = pi;
                                                $.extend( widget, WidgetMethods.dropdownWidget );
                                                if ( Util.apiBool( pi.submodules ) ) {
                                                        widget.getSubmodules = WidgetMethods.submoduleWidget.single;
-                                                       widget.getMenu().on( 'choose', ApiSandbox.updateUI );
+                                                       widget.getMenu().on( 'select', ApiSandbox.updateUI );
+                                               }
+                                               if ( pi.deprecatedvalues ) {
+                                                       widget.getMenu().on( 'select', function ( item ) {
+                                                               this.$element.toggleClass(
+                                                                       'apihelp-deprecated-value',
+                                                                       pi.deprecatedvalues.indexOf( item.data ) >= 0
+                                                               );
+                                                       }, [], widget );
                                                }
                                        }
 
                                widget = new OO.ui.CapsuleMultiselectWidget( {
                                        allowArbitrary: true,
                                        allowDuplicates: Util.apiBool( pi.allowsduplicates ),
+                                       $overlay: $( '#mw-apisandbox-ui' ),
                                        popup: {
                                                classes: [ 'mw-apisandbox-popup' ],
                                                $content: $content
 
                                if ( !formatDropdown ) {
                                        formatDropdown = new OO.ui.DropdownWidget( {
-                                               menu: { items: [] }
+                                               menu: { items: [] },
+                                               $overlay: $( '#mw-apisandbox-ui' )
                                        } );
-                                       formatDropdown.getMenu().on( 'choose', Util.onFormatDropdownChange );
+                                       formatDropdown.getMenu().on( 'select', Util.onFormatDropdownChange );
                                }
 
                                menu = formatDropdown.getMenu();
                                        }
                                } )
                                        .then( null, function ( code, data, result, jqXHR ) {
+                                               var deferred = $.Deferred();
+
                                                if ( code !== 'http' ) {
                                                        // Not really an error, work around mw.Api thinking it is.
-                                                       return $.Deferred()
-                                                               .resolve( result, jqXHR )
-                                                               .promise();
+                                                       deferred.resolve( result, jqXHR );
+                                               } else {
+                                                       // Just forward it.
+                                                       deferred.reject.apply( deferred, arguments );
                                                }
-                                               return this;
-                                       } )
-                                       .fail( function ( code, data ) {
-                                               var details = 'HTTP error: ' + data.exception;
-                                               $result.empty()
-                                                       .append(
-                                                               new OO.ui.LabelWidget( {
-                                                                       label: mw.message( 'apisandbox-results-error', details ).text(),
-                                                                       classes: [ 'error' ]
-                                                               } ).$element
-                                                       );
+                                               return deferred.promise();
                                        } )
-                                       .done( function ( data, jqXHR ) {
+                                       .then( function ( data, jqXHR ) {
                                                var m, loadTime, button, clear,
-                                                       ct = jqXHR.getResponseHeader( 'Content-Type' );
+                                                       ct = jqXHR.getResponseHeader( 'Content-Type' ),
+                                                       loginSuppressed = jqXHR.getResponseHeader( 'MediaWiki-Login-Suppressed' ) || 'false';
 
                                                $result.empty();
+                                               if ( loginSuppressed !== 'false' ) {
+                                                       $( '<div>' )
+                                                               .addClass( 'warning' )
+                                                               .append( Util.parseMsg( 'apisandbox-results-login-suppressed' ) )
+                                                               .appendTo( $result );
+                                               }
                                                if ( /^text\/mediawiki-api-prettyprint-wrapped(?:;|$)/.test( ct ) ) {
                                                        data = JSON.parse( data );
                                                        if ( data.modules.length ) {
                                                                .on( 'click', button.setDisabled, [ true ], button )
                                                                .$element.appendTo( $result );
                                                }
+                                       }, function ( code, data ) {
+                                               var details = 'HTTP error: ' + data.exception;
+                                               $result.empty()
+                                                       .append(
+                                                               new OO.ui.LabelWidget( {
+                                                                       label: mw.message( 'apisandbox-results-error', details ).text(),
+                                                                       classes: [ 'error' ]
+                                                               } ).$element
+                                                       );
                                        } );
                        } );
                },
                                        nooptional: true
                                } );
                                button = new OO.ui.ButtonWidget( {
-                                       icon: 'remove',
+                                       icon: 'trash',
                                        flags: 'destructive'
                                } );
                                layout = new OO.ui.ActionFieldLayout(
 
                Util.fetchModuleInfo( this.apiModule )
                        .done( function ( pi ) {
-                               var prefix, i, j, descriptionContainer, widget, $widgetLabel, widgetField, helpField, tmp, flag, count,
+                               var prefix, i, j, descriptionContainer, widget, widgetField, helpField, tmp, flag, count,
                                        items = [],
                                        deprecatedItems = [],
                                        buttons = [],
                                                        }
                                                );
 
-                                               $widgetLabel = $( '<span>' );
                                                widgetField = new OO.ui.FieldLayout(
                                                        widget,
                                                        {
                                                                align: 'left',
                                                                classes: [ 'mw-apisandbox-widget-field' ],
-                                                               label: prefix + pi.parameters[ i ].name,
-                                                               $label: $widgetLabel
+                                                               label: prefix + pi.parameters[ i ].name
                                                        }
                                                );
 
-                                               // FieldLayout only does click for InputElement
-                                               // widgets. So supply our own click handler.
-                                               $widgetLabel.on( 'click', widgetLabelOnClick.bind( widgetField ) );
+                                               // We need our own click handler on the widget label to
+                                               // turn off the disablement.
+                                               widgetField.$label.on( 'click', widgetLabelOnClick.bind( widgetField ) );
 
                                                // Don't grey out the label when the field is disabled,
                                                // it makes it too hard to read and our "disabled"